﻿using AIVision.Model.EntityModels;
using AIVision.Model.UIModels;
using AIVision.Repository.Entitys;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIVision.Repository.Services
{
    public class InpsectionRecordService
    {
        private readonly AiVisionDbContext dbContext;
        private readonly IMapper mapper;

        public InpsectionRecordService(AiVisionDbContext dbContext, IMapper mapper)
        {
            this.dbContext = dbContext;
            this.mapper = mapper;
        }

        public void SaveRecordAndRefSummary(SummaryDataModel model)
        {
            InpsectionRecordDto dto = new InpsectionRecordDto();
            if (model.AllQty > 0)
            {
                dto.OkQty = model.OkQty;
                dto.NgQty = model.NgQty;
                dto.AllQty = model.AllQty;
                dto.OkRate = model.OkRate;
                dto.FangLiaoQty = model.FangLiaoQty;
                dto.XuanZhuanQty = model.XuanZhuanQty;
                dto.ChaHeQty = model.ChaHeQty;
                dto.FeiLiaoQty = model.FeiLiaoQty;
                dto.ChuFaPaiZhaoQty = model.ChuFaPaiZhaoQty;
                dto.ZhiXingPaiZhaoQty = model.ZhiXingPaiZhaoQty;
                dto.PaiZhaoCalcQty = model.PaiZhaoCalcQty;

                dto.StartTime = model.StartTime;
                dto.EndTime = DateTime.Now;
                dto.Duration = (dto.EndTime - dto.StartTime).Minutes;
                if (dto.AllQty > 0 && dto.Duration > 0)
                    dto.AvgSpeed = Math.Floor((decimal)dto.AllQty / dto.Duration);
                var entity = mapper.Map<InpsectionRecord>(dto);
                dbContext.InpsectionRecords.Add(entity);
                dbContext.SaveChanges();
            }
            model.PathErrQty = 0;
            model.LocationErrQty = 0;
            model.PhotoErrQty = 0;
            model.OkQty = 0;
            model.NgQty = 0;
            model.AllQty = 0;
            model.OkRate = 0;
            model.FangLiaoQty = 0;
            model.XuanZhuanQty = 0;
            model.ChaHeQty = 0;
            model.FeiLiaoQty = 0;
            model.ChuFaPaiZhaoQty = 0;
            model.ZhiXingPaiZhaoQty = 0;
            model.PaiZhaoCalcQty = 0;
            model.StartTime = DateTime.Now;
            model.DefectCount.Clear();
        }

        public ObservableCollection<InpsectionRecordDto> GetRecords(DateTime startTime,DateTime endTime)
        {
            ObservableCollection<InpsectionRecordDto> result = new ObservableCollection<InpsectionRecordDto>();
            var v = dbContext.InpsectionRecords.ToList();
            var entitys = dbContext.InpsectionRecords.Where(m=>m.StartTime>=startTime && m.StartTime <= endTime).ToList();
            foreach (var item in entitys)
            {
                result.Add(mapper.Map<InpsectionRecordDto>(item));
            }
            return result;
        }
    }
}
